#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

#define DIE_IMPLEMENTATION
#include "die.h"
#include "process.h"

int handle_fork(void (*childfunc)(void))
{
    int pid = fork();
    switch(pid)
    {
        case 0:
            childfunc();
            _exit(0);
        case -1:
            die(1, "FATAL: could not fork process");
        default:
            return pid;
    }
}

void wait_for_child(void)
{
    int cstatus;
    int cpid = wait(&cstatus);
    if (WEXITSTATUS(cstatus))
        die(1, "ERROR: child %d exited with code %d", cpid, WEXITSTATUS(cstatus));
}
